package fons.navigator.views.island;

import java.util.Map;

import com.google.common.collect.Maps;

import fons.model.ontology.SelectionDefinition;

public class Cluster {
	private final Map<Island, Double> islands = Maps.newHashMap();
	private final VisualIsland visualIsland;
	private String name;
	private SelectionDefinition selection;
	
	public Cluster (VisualIsland visualIsland, String name) {
		this.visualIsland = visualIsland;
		this.name = name;
	}

	public void addIsland (Island island, double match) {
		assert(match >= 0 && match <= 1);
		islands.put(island, match);
	}

	public void addIsland (Island island) {
		addIsland(island, 1);
	}

	public void removeIsland (Island island) {
		this.islands.remove(island);
	}

	public String getName() {
		return name;
	}

	public void setName (String name) {
		this.name = name;
	}

	public boolean contains (Island island) {
		return islands.containsKey(island);
	}

	public double includes (Island island) {
		if (this.contains(island)) return islands.get(island);
		else return 0;
	}

	public void defineSelection () {
		if (selection == null) {
			selection = visualIsland.getView().getContext().defineSelection(getName());
			for (Island island: islands.keySet()) {
				selection.add(island.getIslandSpace().getDimensions().iterator().next());
			}
		}
	}
	
	public SelectionDefinition getSelection() {
		if (selection == null) defineSelection();
		return selection;
	}
}
